package com.lfnyxy.service.imp;

import com.lfnyxy.common.dto.ApiResult;
import com.lfnyxy.common.dto.CodeMessage;
import com.lfnyxy.dao.ClientUserDao;
import com.lfnyxy.entity.ClientUserEntity;
import com.lfnyxy.query.ClientUserQuery;
import com.lfnyxy.service.ClientUserService;
import com.lfnyxy.util.MD5Service;
import com.lfnyxy.util.UUIDUtil;
import com.lfnyxy.vo.ClientUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientUserServiceImp implements ClientUserService {
    @Autowired
    public ClientUserDao clientUserDao;

    public int CreateItem(ClientUserEntity entity) {
        if (entity != null) {
            //较验帐号是否存在。
            ClientUserEntity clientUserEntity = clientUserDao.QueryItemByAccount(entity.getAccount());
            if(clientUserEntity != null){
                return -1;
            }else {
                String salt = UUIDUtil.uuid();
                entity.setSalt(salt);

                String mPass = MD5Service.formPassToDbPass(entity.getPassword(), salt);
                entity.setPassword(mPass);
                return clientUserDao.CreateItem(entity);
            }
        } else {
            return 0;
        }
    }

    public int UpdateItem(ClientUserEntity entity) {
        if (entity != null) {
            return clientUserDao.UpdateItem(entity);
        } else {
            return 0;
        }
    }

    public List <ClientUserVo> QueryPageList(ClientUserQuery query) {
        int count = clientUserDao.QueryPageListCount(query);
        query.setTotal(count);

            return clientUserDao.QueryPageList(query);

    }

    public List <ClientUserEntity> QueryAll() {
        return clientUserDao.QueryAll();
    }

    public ClientUserEntity QueryItem(int Id) {
        if (Id > 0) {
            return clientUserDao.QueryItem(Id);
        } else {
            return null;
        }
    }

    public int DeleteItem(int Id) {
        if (Id > 0) {
            return clientUserDao.DeleteItem(Id);
        } else {
            return 0;
        }
    }

    private boolean CheckIsExist(ClientUserEntity entity) {
        if (entity != null) {
            return clientUserDao.CheckIsExist(entity) > 0;
        } else {
            return false;
        }
    }
}